#include "PcsData.h"

#include <float.h>
#include <limits.h>

#include "ComDefine.h"

bool CPcsData::IsCommNormal()
{
    return (0 == m_uCommState);
}

bool CPcsData::IsCommFault()
{
    return (1 == m_uCommState);
}

bool CPcsData::IsRunning()
{
    return (0 == m_uRunState);
}

bool CPcsData::IsStopped()
{
    return (1 == m_uRunState);
}

bool CPcsData::IsFault()
{
    return (1 == m_uFaultState);
}

bool CPcsData::IsNoFault()
{
    return (0 == m_uFaultState);
}

bool CPcsData::IsAlarm()
{
    return (1 == m_uAlarmState);
}

bool CPcsData::IsNoAlarm()
{
    return (0 == m_uAlarmState);
}

bool CPcsData::IsOpening()
{
    return (1 == m_uSwitchState);
}

bool CPcsData::IsClosed()
{
    return (0 == m_uSwitchState);
}

//获取***的显示文本
QString CPcsData::GetCommStateText()
{
    if (0xFFFF == m_uCommState)
    {
        return QObject::tr("---");
    }
    else if (1 == m_uCommState)
    {
        return QObject::tr("故障");
    }
    else
    {
        return QObject::tr("正常");
    }
}

//系统启动
void CPcsData::StartSystem()
{
    m_uSetSysToWork = 1; //系统启动
    m_uSetSysToWorkCopy = 1;
}

//停止系统
void CPcsData::StopSystem()
{
    m_uSetSysToWork = 2; //系统停止
    m_uSetSysToWorkCopy = 2;
}

//获取运行模式的中文utf8(主要供界面显示)
QString CPcsData::GetUtf8OfRunMode()
{
    QString ret = "\xe5\x88\x9d\xe5\xa7\x8b\xe7\x8a\xb6\xe6\x80\x81"; //初始状态

    if (1 == this->m_uRunMode)
    {
        ret = "\xE5\xB0\xB1\xE5\x9C\xB0\xE6\x89\x8B\xE5\x8A\xA8"; //就地手动
    }
    else if (2 == this->m_uRunMode)
    {
        ret = "\xE5\xB0\xB1\xE5\x9C\xB0\xE6\x9B\xB2\xE7\xBA\xBF"; //就地曲线
    }
    else if (3 == this->m_uRunMode)
    {
        ret = "\xe5\xb0\xb1\xe5\x9c\xb0\xe8\x87\xaa\xe5\x8a\xa8"; //就地自动
    }
    else if (4 == this->m_uRunMode)
    {
        ret = "\xe8\xbf\x9c\xe7\xa8\x8b\xe6\x9b\xb2\xe7\xba\xbf"; //远程曲线
    }

    return ret;
}

QString CPcsData::GetRunModeName()
{
    QString ret = "初始状态"; //初始状态

    if (1 == this->m_uRunMode)
    {
        ret = "就地手动"; //就地手动
    }
    else if (2 == this->m_uRunMode)
    {
        ret = "就地曲线"; //就地曲线
    }
    else if (3 == this->m_uRunMode)
    {
        ret = "就地自动"; //就地自动
    }
    else if (4 == this->m_uRunMode)
    {
        ret = "远程曲线"; //远程曲线
    }

    return ret;
}

// //是否需要下设故障复位,true需要下设，false不需要下设
// bool CPcsData::IsSetPcsFaultReset()
// {
//    if (1 == this->usReqPcsToReset)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
// }

// //是否需要下设启机,true需要下设，false不需要下设
// bool CPcsData::IsSetPcsToStart()
// {
//    if (1 == this->usReqPcsToStart)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
// }

// //是否需要下设停机,true需要下设，false不需要下设
// bool CPcsData::IsSetPcsToStop()
// {
//     if (1 == this->usReqPcsToStop)
//     {
//         return true;
//     }
//     else
//     {
//         return false;
//     }
// }

// //是否需要下设待机,true需要下设，false不需要下设
// bool CPcsData::IsSetPcsToWait()
// {
//    if (1 == this->usReqPcsToWait)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
// }

// //清除下设故障复位命令
// void CPcsData::ClearCmdSetFaultReset()
// {
//    this->usReqPcsToReset = 0;
// }

// //清除下设启机命令
// void CPcsData::ClearCmdSetPcsToStart()
// {
//    this->usReqPcsToStart = 0;
// }

// //清除下设停机命令
// void CPcsData::ClearCmdSetPcsToStop()
// {
//    this->usReqPcsToStop = 0;
// }

// //清除下设待机命令
// void CPcsData::ClearCmdSetPcsToWait()
// {
//    this->usReqPcsToWait = 0;
// }

// //是否需要下设有功功率,true需要下设，false不需要下设
// bool CPcsData::IsSetPcsActPower()
// {
//    if (0xFFFF == (int)this->fSetManuActPower)
//    {
//        //无效值，不允许下设给PCS，避免重复下设
//        return false;
//    }
//    else
//    {
//        //有效值,>0充电，<0放电，=0零功率待机
//        return true;
//    }
// }

// bool CPcsData::IsValidSetPcsActPower()
// {
//     if (0xFFFF == (int)this->fSetManuActPower)
//     {
//         //无效值，避免重复下设
//         return false;
//     }
//     else
//     {
//         //有效值,>0充电，<0放电，=0零功率待机
//         return true;
//     }
// }

// bool CPcsData::IsInvalidSetPcsActPower()
// {
//     if (0xFFFF == (int)this->fSetManuActPower)
//     {
//         //无效值，不允许下设给PCS，避免重复下设
//         return true;
//     }
//     else
//     {
//         //有效值,>0充电，<0放电，=0零功率待机
//         return false;
//     }
// }

// //是否需要下设无功功率,true需要下设，false不需要下设
// bool CPcsData::IsSetPcsReactPower()
// {
//     if (0xFFFF == (int)this->fSetManuReactPower)
//     {
//         //无效值，不允许下设给PCS，避免重复下设
//         return false;
//     }
//     else
//     {
//         //有效值,>0充电，<0放电，=0零功率待机
//         return true;
//     }
// }

// bool CPcsData::IsValidSetPcsReactPower()
// {
//     if (0xFFFF == (int)this->fSetManuReactPower)
//     {
//         //无效值，不允许下设给PCS，避免重复下设
//         return false;
//     }
//     else
//     {
//         //有效值,>0充电，<0放电，=0零功率待机
//         return true;
//     }
// }

// bool CPcsData::IsInvalidSetPcsReactPower()
// {
//     if (0xFFFF == (int)this->fSetManuReactPower)
//     {
//         //无效值，不允许下设给PCS，避免重复下设
//         return true;
//     }
//     else
//     {
//         //有效值,>0充电，<0放电，=0零功率待机
//         return false;
//     }
// }

void CPcsData::InitToInvalid()
{
    InitToValue(0xFFFF, FLT_MAX);
}

void CPcsData::InitToZero()
{
    InitToValue(0, 0.0);
}

void CPcsData::InitToValue(quint16 uvalue, float fvalue)
{
    m_uCommState = uvalue;
    m_uReset = uvalue;
    m_uStart = uvalue;
    m_uStop = uvalue;
    m_uStandby = uvalue;
    for (int i = 0; i < ARRAYSIZE(m_uRev); ++i)
    {
        m_uRev[i] = uvalue;
    }

    m_fSetYgPower = fvalue;
    m_fSetWgPower = fvalue;
    for (int i = 0; i < ARRAYSIZE(m_fRev); ++i)
    {
        m_fRev[i] = fvalue;
    }

    //转发的100个UINT16数据
    m_uRunState = uvalue;
    m_uFaultState = uvalue;
    m_uAlarmState = uvalue;
    m_uSwitchState = uvalue;

    m_uOnOffGridState = uvalue;
    //故障字
    m_uWordHd = uvalue;
    m_uWordHdSec = uvalue;
    m_uWordNet = uvalue;
    m_uWordMother = uvalue;

    m_uWordAcCap = uvalue;
    m_uWordSys = uvalue;
    m_uWordSwitch = uvalue;
    m_uWordOther = uvalue;
    m_uRunMode = uvalue;

    m_uStopState = uvalue;
    m_uPhaseOrder = uvalue;

    m_uSetSysToWork = uvalue;
    m_uSetPcsToReset = uvalue;
    m_uSetPcsToStart = uvalue;
    m_uSetPcsToStop = uvalue;
    m_uSetPcsToWait = uvalue;

    m_uSetSysToWorkCopy = uvalue;
    m_uSetPcsToResetCopy = uvalue;
    m_uSetPcsToStartCopy = uvalue;
    m_uSetPcsToStopCopy = uvalue;
    m_uSetPcsToWaitCopy = uvalue;

    for (int i = 0; i < ARRAYSIZE(m_uData); ++i)
    {
        m_uData[i] = uvalue;
    }

    //转发的100个FLOAT数据
    //直流
    m_fDirectVol = fvalue;
    m_fDirectCur = fvalue;
    m_fDirectActPower = fvalue;
    m_fDirectReactPower = fvalue;
    m_fDeviceTemp = fvalue;

    //相电压
    m_fAPhaseVol = fvalue;
    m_fBPhaseVol = fvalue;
    m_fCPhaseVol = fvalue;
    m_fTotalPhaseVol = fvalue;

    //线电压
    m_fABLineVol = fvalue;
    m_fBCLineVol = fvalue;
    m_fCALineVol = fvalue;
    m_fTotalLineVol = fvalue;

    //相电流
    m_fAPhaseCur = fvalue;
    m_fBPhaseCur = fvalue;
    m_fCPhaseCur = fvalue;
    m_fTotalPhaseCur = fvalue;

    //有功功率ActivePower
    m_fAPhaseActPower = fvalue;
    m_fBPhaseActPower = fvalue;
    m_fCPhaseActPower = fvalue;
    m_fTotalActPower = fvalue;

    //无功功率ReactivePower
    m_fAPhaseReactPower = fvalue;
    m_fBPhaseReactPower = fvalue;
    m_fCPhaseReactPower = fvalue;
    m_fTotalReactPower = fvalue;

    //视在功率ApparentPower
    m_fAPhaseApprPower = fvalue;
    m_fBPhaseApprPower = fvalue;
    m_fCPhaseApprPower = fvalue;
    m_fTotalApprPower = fvalue;

    //功率因素PowerFactor
    m_fAPhasePowerFactor = fvalue;
    m_fBPhasePowerFactor = fvalue;
    m_fCPhasePowerFactor = fvalue;
    m_fTotalPowerFactor = fvalue;

    //频率
    m_fAPhaseFreq = fvalue;
    m_fBPhaseFreq = fvalue;
    m_fCPhaseFreq = fvalue;
    m_fTotalFreq = fvalue;

    //电能
    m_fPosActPowerEq = fvalue;
    m_fNegActPowerEq = fvalue;
    m_fPosReactPowerEq = fvalue;
    m_fNegReactPowerEq = fvalue;

    //总电量
    m_fAcTotalChgEq = fvalue;
    m_fAcTotalDisChgEq = fvalue;
    m_fDcTotalChgEq = fvalue;
    m_fDcTotalDisChgEq = fvalue;

    //日电量
    m_fCurDayZeroChgEq = fvalue;
    m_fCurDayZeroDisChgEq = fvalue;
    m_fCurDayChgEq = fvalue;
    m_fCurDayDisChgEq = fvalue;
    m_fLastDayChgEq = fvalue;
    m_fLastDayDisChgEq = fvalue;

    //周电量
    m_fCurWeekZeroChgEq = fvalue;
    m_fCurWeekZeroDisChgEq = fvalue;
    m_fCurWeekChgEq = fvalue;
    m_fCurWeekDisChgEq = fvalue;
    m_fLastWeekChgEq = fvalue;
    m_fLastWeekDisChgEq = fvalue;
    //月电量
    m_fCurMonthZeroChgEq = fvalue;
    m_fCurMonthZeroDisChgEq = fvalue;
    m_fCurMonthChgEq = fvalue;
    m_fCurMonthDisChgEq = fvalue;
    m_fLastMonthChgEq = fvalue;
    m_fLastMonthDisChgEq = fvalue;
    //年电量
    m_fCurYearZeroChgEq = fvalue;
    m_fCurYearZeroDisChgEq = fvalue;
    m_fCurYearChgEq = fvalue;
    m_fCurYearDisChgEq = fvalue;
    m_fLastYearChgEq = fvalue;
    m_fLastYearDisChgEq = fvalue;

    //额定参数
    m_fRatedPower = fvalue;
    m_fRatedVol = fvalue;
    m_fRatedCur = fvalue;
    m_fRatedFreq = fvalue;

    //PCS充电限额
    m_fPcsMaxChgActPower = fvalue;
    m_fPcsMaxChgReactPower = fvalue;
    m_fPcsMaxChgVol = fvalue;
    m_fPcsMaxChgCur = fvalue;

    //PCS放电限额
    m_fPcsMaxDisChgActPower = fvalue;
    m_fPcsMaxDisChgReactPower = fvalue;
    m_fPcsMaxDisChgVol = fvalue;
    m_fPcsMaxDisChgCur = fvalue;

    //支路充电限额
    m_fTagMaxChgActPower = fvalue;
    m_fTagMaxChgReactPower = fvalue;
    m_fTagMaxChgVol = fvalue;
    m_fTagMaxChgCur = fvalue;

    //支路放电限额
    m_fTagMaxDisChgActPower = fvalue;
    m_fTagMaxDisChgReactPower = fvalue;
    m_fTagMaxDisChgVol = fvalue;
    m_fTagMaxDisChgCur = fvalue;

    //温度
    m_fAPhaseTemp = fvalue;
    m_fBPhaseTemp = fvalue;
    m_fCPhaseTemp = fvalue;
    m_fTotalTemp = fvalue;
    m_fElecNetFreq = fvalue;

    for (int i = 0; i < ARRAYSIZE(m_fData); ++i)
    {
        m_fData[i] = fvalue;
    }

    m_uSetSwitchState = uvalue;
    m_uSetSwitchStateCopy = uvalue;
    m_uSetRunMode = uvalue;
    m_uSetRunModeCopy = uvalue;
    m_uSetOnOffGridState = uvalue;
    m_uSetOnOffGridStateCopy = uvalue;
    m_fSetActPower = fvalue;
    m_fSetActPowerCopy = fvalue;
//    m_fSetDisChgActPower = fvalue;
//    m_fSetDisChgActPowerCopy = fvalue;
    m_fSetReactPower = fvalue;
    m_fSetReactPowerCopy = fvalue;
//    m_fSetDisChgReactPower = fvalue;
//    m_fSetDisChgReactPowerCopy = fvalue;
    m_uSetFaultReset = uvalue;
    m_uSetFaultResetCopy = uvalue;
    m_uSetBalanceMode = uvalue;
    m_uSetBalanceModeCopy = uvalue;
    m_fSetManuActPower = fvalue;
    m_fSetManuActPowerCopy = fvalue;
    m_fSetManuReactPower = fvalue;
    m_fSetManuReactPowerCopy = fvalue;

    for (int i = 0; i < ARRAYSIZE(m_uSetData); ++i)
    {
        m_uSetData[i] = uvalue;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetDataCopy); ++i)
    {
        m_uSetDataCopy[i] = uvalue;
    }
}
